/*
 * TapisView.java
 *
 * Created on 19 décembre 2007, 14:13
 */
package ourpkr.view;

import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Vector;
import ourpkr.OurPkrApp;
import ourpkr.model.Carte;
import ourpkr.model.EnsembleCartes;

/**
 * Vue du Tapis de Jeu
 * 
 * @author  niboucha
 */
public class TapisView extends javax.swing.JPanel {

    /** Creates new form TapisView */
    public TapisView() {
        initComponents();
        initCarts();
        this.tapisLbl.setText("");
        this.potLbl.setText("");
        OurPkrApp.getApplication().getPc().pcs.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent arg0) {
                if (arg0.getPropertyName().equals("tour")) {
                    newTour();
                } else if (arg0.getPropertyName().equals("tourNb")) {
                    tourNbLegend.setText("Tour n°" + arg0.getNewValue());
                } else if (arg0.getPropertyName().equals("tourFini")) {
                    nextTour.setEnabled(true);
                }
            }
        });
    }
    
    /**
     * Preparation de l'affichage des cartes
     */
    private void initCarts() {
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ourpkr.OurPkrApp.class).getContext().getResourceMap(Joueur.class);
        carteFactori fact = new carteFactori(Joueur.class.getResource("/" + resourceMap.getResourcesDir() + resourceMap.getString("carteFile")).toString());
        cartes = fact.fact(5);
        Dimension d = new Dimension((int) cartes.elementAt(0).getAW() * 5, (int) cartes.elementAt(0).getAH());
        cartesPanel.setMaximumSize(d);
        cartesPanel.setMinimumSize(d);
        cartesPanel.setPreferredSize(d);
        cartesPanel.setSize(d);
        for (aCarte c : cartes) {
            c.setVisible(false);
            cartesPanel.add(c);
        }
    }

    /**
     * Mise en place des listener sur le nouveau Tour
     */
    private void newTour() {
        //Listener pour le pot
        OurPkrApp.getApplication().getPc().getTour().pcs.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent arg0) {
                if (arg0.getPropertyName().equals("mainPot")) {
                    potLbl.setText(arg0.getNewValue().toString());
                } else if (arg0.getPropertyName().equals("tapis")) {

                    //Listener pour les cartes du Tapis
                    ((EnsembleCartes) arg0.getNewValue()).pcs.addPropertyChangeListener("cartes", new PropertyChangeListener() {

                        public void propertyChange(PropertyChangeEvent arg0) {
                            Vector<Carte> c = (Vector<Carte>) (arg0.getNewValue());
                            int i = 0;
                            for (; i < c.size() && i < 5; i++) {
                                cartes.elementAt(i).setColor(c.elementAt(i).getCouleur() - 1);
                                cartes.elementAt(i).setVal(c.elementAt(i).getValeur() - 1);
                                cartes.elementAt(i).setVisible(true);
                            }
                            for (; i < 5; i++) {
                                cartes.elementAt(i).setVisible(false);
                            }
                        }
                    });

                    //Cacher les cartes
                    for (aCarte c : cartes) {
                        c.setVisible(false);
                    }
                } else if (arg0.getPropertyName().equals("progress")) {
                    coup.setText(arg0.getNewValue().toString());
                } else if (arg0.getPropertyName().equals("partieRes")) {
                    partieRes.setText(arg0.getNewValue().toString());
                }
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tapisNameLbl = new javax.swing.JLabel();
        tapisLbl = new javax.swing.JLabel();
        potNameLbl = new javax.swing.JLabel();
        potLbl = new javax.swing.JLabel();
        tourNbLegend = new javax.swing.JLabel();
        nextTour = new javax.swing.JButton();
        coup = new javax.swing.JLabel();
        partieRes = new javax.swing.JLabel();
        cartesPanel = new javax.swing.JPanel();
        emptyPane = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ourpkr.OurPkrApp.class).getContext().getResourceMap(TapisView.class);
        tapisNameLbl.setText(resourceMap.getString("tapisNameLbl.text")); // NOI18N
        tapisNameLbl.setName("tapisNameLbl"); // NOI18N

        tapisLbl.setText(resourceMap.getString("tapisLbl.text")); // NOI18N
        tapisLbl.setName("tapisLbl"); // NOI18N

        potNameLbl.setIcon(resourceMap.getIcon("potNameLbl.icon")); // NOI18N
        potNameLbl.setName("potNameLbl"); // NOI18N

        potLbl.setText(resourceMap.getString("potLbl.text")); // NOI18N
        potLbl.setName("potLbl"); // NOI18N

        tourNbLegend.setText(resourceMap.getString("tourNbLegend.text")); // NOI18N
        tourNbLegend.setName("tourNbLegend"); // NOI18N

        nextTour.setText(resourceMap.getString("nextTour.text")); // NOI18N
        nextTour.setEnabled(false);
        nextTour.setName("nextTour"); // NOI18N
        nextTour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextTourActionPerformed(evt);
            }
        });

        coup.setText(resourceMap.getString("coup.text")); // NOI18N
        coup.setName("coup"); // NOI18N

        partieRes.setText(resourceMap.getString("partieRes.text")); // NOI18N
        partieRes.setName("partieRes"); // NOI18N

        cartesPanel.setName("cartesPanel"); // NOI18N
        cartesPanel.setLayout(new javax.swing.BoxLayout(cartesPanel, javax.swing.BoxLayout.LINE_AXIS));

        emptyPane.setName("emptyPane"); // NOI18N

        javax.swing.GroupLayout emptyPaneLayout = new javax.swing.GroupLayout(emptyPane);
        emptyPane.setLayout(emptyPaneLayout);
        emptyPaneLayout.setHorizontalGroup(
            emptyPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        emptyPaneLayout.setVerticalGroup(
            emptyPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 83, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tapisNameLbl)
                    .addComponent(potNameLbl))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(potLbl)
                    .addComponent(tapisLbl))
                .addGap(41, 41, 41)
                .addComponent(emptyPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cartesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(55, 55, 55)
                .addComponent(tourNbLegend)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(coup)
                        .addGap(95, 95, 95)
                        .addComponent(nextTour))
                    .addComponent(partieRes))
                .addGap(118, 118, 118))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(cartesPanel, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(tapisNameLbl)
                                .addComponent(tapisLbl)
                                .addComponent(tourNbLegend)
                                .addComponent(coup)
                                .addComponent(nextTour))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(potLbl)
                                    .addComponent(partieRes))
                                .addComponent(potNameLbl))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(emptyPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(17, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void nextTourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTourActionPerformed
        nextTour.setEnabled(false);
        OurPkrApp.getApplication().getPc().nextTour();
    }//GEN-LAST:event_nextTourActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel cartesPanel;
    private javax.swing.JLabel coup;
    private javax.swing.JPanel emptyPane;
    private javax.swing.JButton nextTour;
    private javax.swing.JLabel partieRes;
    private javax.swing.JLabel potLbl;
    private javax.swing.JLabel potNameLbl;
    private javax.swing.JLabel tapisLbl;
    private javax.swing.JLabel tapisNameLbl;
    private javax.swing.JLabel tourNbLegend;
    // End of variables declaration//GEN-END:variables
    
    /**
     * Liste des 5 visuelles correspondants aux cartes
     */
    private Vector<aCarte> cartes;
}
